package dao;

import java.sql.Connection;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import model.Student;
import model.Subject;
import model.Teacher;
import ui.StudentFrame;
import util.JDBCUtil;


public  class StudentDaoJDBCImpl implements StudentDao{

	

	

	@Override
	public void getmycourse(int id) {
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from student where id like ?";//表中有id和name这列
		try (Connection conn = JDBCUtil.getConnection()) {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			ResultSetMetaData data = rs.getMetaData();
			int colum = rs.getMetaData().getColumnCount();
			String[] columnamestr = new String[colum];
			double[] source = new double[colum];
			SubjectDao sub = new SubjectDaoJDBCImpl();
			for(int i = 7;i <= colum;i++) {
				columnamestr[i-1] = data.getColumnName(i);
				System.out.println(data.getColumnName(i));
			}
			while(rs.next()){
				int stuid = rs.getInt("id");
				String stuname = rs.getString("name");
				String clacces = rs.getString("class");
				String gender = rs.getString("gender");
				String birthdate = rs.getString("birthdate");
				Student stu = new Student(stuid,stuname,clacces,gender,birthdate);
				StudentFrame.studentinf = stu;
				for(int j = 6;j < colum;j++) {
					source[j] = rs.getDouble(columnamestr[j]);
					System.out.println(source[j]);
				}
			}
			for(int k = 6;k < colum;k++) {
				if(source[k]!=-1) {
					Teacher teacher = new Teacher(sub.getteachername(columnamestr[k]));
					StudentFrame.mycourselist.add(new Subject(columnamestr[k],teacher));
				}
			}
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	

	@Override
	public boolean isLogin(String identity, int id, String password) {
		boolean logincheck = false;
		Statement stat = null;
		ResultSet rs = null;
		String sql = "select * from student";
		try (Connection conn = JDBCUtil.getConnection()) {
			stat = conn.createStatement();
			rs = stat.executeQuery(sql);
			while (rs.next()) {
				int stuid = rs.getInt("id");
				String pwd = rs.getString("password");
				if(id==stuid) {
					if(password.equals(pwd)) {
						logincheck = true;
						System.out.println("该学生用户为："+id);
						break;
					}
				}
			}
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return logincheck;
	}

	@Override
	public void getChoosecourse(int id) {
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from student where id like ?";//表中有id和name这列
		try (Connection conn = JDBCUtil.getConnection()) {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			ResultSetMetaData data = rs.getMetaData();
			int colum = rs.getMetaData().getColumnCount();
			String[] columnamestr = new String[colum];
			double[] source = new double[colum];
			SubjectDao sub = new SubjectDaoJDBCImpl();
			for(int i = 7;i <= colum;i++) {
				columnamestr[i-1] = data.getColumnName(i);
				System.out.println(data.getColumnName(i));
			}
			while(rs.next()){
				for(int j = 6;j < colum;j++) {
					source[j] = rs.getDouble(columnamestr[j]);
					System.out.println(source[j]);
				}
			}
			for(int k = 6;k < colum;k++) {
				if(source[k]==-1) {
					Teacher teacher = new Teacher(sub.getteachername(columnamestr[k]));
					StudentFrame.choosecourselist.add(new Subject(columnamestr[k],teacher));
				}
			}
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}



	@Override
	public boolean choosecourse(int id,String coursename) {
		boolean check = false;
		PreparedStatement pstat = null;
		int result = -1;
		String sql = "update student set "+coursename+"='0' where id="+id;
		try (Connection conn = JDBCUtil.getConnection()) {
			pstat = conn.prepareStatement(sql);
			result = pstat.executeUpdate(sql);
			if(result>0) check = true;
			
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return check;
	}
	
	

}
